<html>
    <head>
        <title>
            Salary Calculation.
        </title>
        <script type="text/javascript">

        <!--
            var compute = function(){
                console.log("start ..............");
                var records = getRecords();
                records = sortRecords(records)
                var salariesBeforeTaxReduction = calculateSalary(records);
                reduceTax(salariesBeforeTaxReduction);
                console.log("..............  end.");
            };

            var sortRecords = function(records){
                records.sort(function(a,b){return a.baseSalary-b.baseSalary;});
                return records;
            };

            var calculateSalary = function(records){
                result = [];
                for (var i=0; i<records.length ; i++){
                    var record = records[i];
                    var salary = {};
                    salary.tax = .1;
                    try{
                       //record.lastName.length;
                       salary.value = record.baseSalary + record.bonusPoint*100 + record.married*3000 + record.children*4000 + record.senior*10000;
                       if (salary.value<90000 && record.age > 50){
                           salary.tax = 0;}
                    }catch(exc){
                       //ignore
                    }
                    result.push(salary);
                }
                return result;
            };

            var reduceTax = function(salariesBeforeTaxReduction){

                for (var i=0; i<salariesBeforeTaxReduction.length; i++){
                    var salary = salariesBeforeTaxReduction[i];
                    var finalSalary = {};
                    finalSalary.value = salary.value - salary.tax*salary.value;
                    if  (finalSalary.value >= 120000){
                        // do nothing
                        //throw new Error("Warning : Salary is higher than maximum limit : " + finalSalary.value + " >= 120000");
                    } else if (finalSalary.value < 80000){
                        throw new Error("Error : Salary is lower than minimum limit : " + finalSalary.value + " < 80000");
                    } else {
                        console.log(i + ") The salary amount is : " + finalSalary.value)
                    }
                }
            };

            var getRecords = function(){
                var records = [];
                var record;
                record = {name:"A", company:"IBM", baseSalary: 92000 ,senior:1, married:0, children: 0,  age:44, bonusPoint:220};
                records.push(record);
                record = {name:"B", company:"GOOGLE", baseSalary: 88000 ,senior:1, married:0, children: 0, age:34, bonusPoint:25};
                records.push(record);
                record = {name:"C", company:"MICROSOFT", baseSalary:90000 ,senior:1, married:1, children: 0, age:40, bonusPoint:100};
                records.push(record);
                record = {name:"E", company:"INTEL", baseSalary: 90000 ,senior:0, married:0, children: 1, age:54, bonusPoint:70};
                records.push(record);
                record = {name:"F", company:"IBM", baseSalary: 89000 ,senior:1, married:1, children: 0, age:44, bonusPoint:350};
                records.push(record);
                record = {name:"G", company:"IBM", baseSalary: 91000 ,senior:0, married:1, children: 3, age:50, bonusPoint:150};
                records.push(record);
                record = {name:"H", company:"INTEL", baseSalary: 88000 ,senior:0, married:0, children: 0, age:28, bonusPoint:20};
                records.push(record);
                record = {name:"I", company:"GOOGLE", baseSalary: 85000 ,senior:1, married:0, children: 3, age:39, bonusPoint:250};
                records.push(record);
                record = {name:"J", company:"YAHOO", baseSalary: 91000 ,senior:0, married:1, children: 2, age:36, bonusPoint:300};
                records.push(record);
                record = {name:"K", company:"HP", baseSalary: 84000 ,senior:0, married:1, children: 0, ages:54, bonusPoint:10};
                records.push(record);
                record = {name:"L", company:"YAHOO", baseSalary: 83000 ,senior:0, married:0, children: 0, age:61, bonusPoint:90};
                records.push(record);
                record = {name:"M", company:"IBM", baseSalary: 85000 ,senior:1, married:0, children: 0,  age:29, bonusPoint:0};
                records.push(record);
                return records;
            };

        -->
        </script>

    </head>

    <body id="myBody">
    <h3>
         Salary Calculation.
    </h3>
    <p>
        This page calculates salaries of a list of employees.
        Go through these steps:
       <ol>
       <li> Click on the button.</li>
       <li> You see an error in Firebug console, set a breakpoint on the error (red dot), then re-run to halt on the error message .</li>
       <li> Locate the defect causes the error.</li>
       </ol>
    </p>


    <button id="reproducer" onclick="compute()">
      Calculate Salaries!
    </button>

    <p id>

    </p>
    </body>

</html>